<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产线名称" prop="lineName" label-width="120">
        <el-input
          v-model="queryParams.lineName"
          placeholder="请输入生产线名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工位名称" prop="gwName">
        <el-input
          v-model="queryParams.gwName"
          placeholder="请输入工位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产线工艺" >
        <el-select v-model="queryParams.lineGy" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建日期" prop="createDate">
        <el-date-picker
          v-model="queryParams.createDate"
          type="date"
          placeholder="选择日期"
          style="width: 205px"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handlePush"-->
<!--        >读取</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pcplan:processproPlan:edit']"
        >编辑</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pcplan:processproPlan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pcplan:processproPlan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table height="calc(100vh - 300px)" v-loading="loading" :data="processproPlanList" @selection-change="handleSelectionChange" :row-class-name="tableRowClassName">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司代码" align="center" prop="factoryCode" />
      <el-table-column label="公司名称" align="center" prop="factoryName" />
      <el-table-column label="订单号" align="center" prop="orderNo" width="160"/>
      <el-table-column label="合同号" align="center" prop="contractNo" width="160"/>
      <el-table-column label="客户合同号" align="center" prop="clientContractNo" width="160">
        <template slot-scope="scope">
          <span v-if="scope.row.clientContractNo==null">--</span>
        </template>
      </el-table-column>
      <el-table-column label="排产编号" align="center" prop="pcCode" width="160"/>
      <el-table-column label="生产线代码" align="center" prop="lineCode" width="130"/>
      <el-table-column label="生产线名称" align="center" prop="lineName" width="130"/>
      <el-table-column label="产品类型" align="center" prop="proType" />
      <el-table-column label="物料编号" align="center" prop="dyProCode" width="260" show-overflow-tooltip/>
      <el-table-column label="物料名称" align="center" prop="proName" width="160" show-overflow-tooltip/>
      <el-table-column label="倒角" align="center" prop="dj" width="120"/>
      <el-table-column label="水线" align="center" prop="sx" width="120"/>
      <el-table-column label="密封面倒角" align="center" prop="mfmdj" width="120"/>
      <el-table-column label="排产日期" align="center" prop="pcDate" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pcDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生产日期" align="center" prop="scDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="交货日期" align="center" prop="jhDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jhDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="物料识别号" align="center" prop="itemIdentifyCode" width="230" show-overflow-tooltip/>
      <el-table-column label="订单数量" align="center" prop="orderNum" />
      <el-table-column label="未排产订单数量" align="center" prop="noPcNum" width="160"/>
      <el-table-column label="排产数量" align="center" prop="pcNum" />
      <el-table-column label="完成数量" align="center" prop="finishComNum" />
      <el-table-column label="外协类型" align="center" prop="outsourType" >
        <template slot-scope="scope">
          <span v-if="scope.row.outsourType=='K'">料块</span>
          <span v-if="scope.row.outsourType=='P'">毛坯</span>
          <span v-if="scope.row.outsourType=='B'">半成品</span>
          <span v-if="scope.row.outsourType=='C'">成品</span>
        </template>
      </el-table-column>
      <el-table-column label="外协数量" align="center" prop="wxNum" />
      <el-table-column label="计划上线时间" align="center" prop="planOnlineTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planOnlineTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划下线时间" align="center" prop="planOfflineTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planOfflineTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="说明" align="center" prop="content"  width="260" show-overflow-tooltip/>
      <el-table-column label="创建人" align="center" prop="createBy" width="120"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160"/>
      <el-table-column label="修改人" align="center" prop="updateBy" width="120"/>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="160"/>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-show="scope.row.isRead=='0'"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pcplan:processproPlan:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            v-show="scope.row.isRead=='0'"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pcplan:processproPlan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改加工排产计划对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="排产编号" prop="pcCode">
              <el-input v-model="form.pcCode" placeholder="请输入排产编号" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料编号" prop="dyProCode">
              <el-input v-model="form.dyProCode" placeholder="请输入对应产品编号" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="物料名称" prop="proName">
              <el-input v-model="form.proName" placeholder="请输入产品名称" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排产数量" prop="pcNum">
              <el-input v-model="form.pcNum" placeholder="请输入排产数量"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产日期" prop="scDate">
              <el-date-picker clearable
                              v-model="form.scDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择生产日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="选择生产线" prop="lineCode">
              <el-select v-model="form.lineCode" placeholder="选择生产线" clearable>
                <el-option
                  v-for="item in this.lineList"
                  :key="item.lineId"
                  :label="item.lineName"
                  :value="item.lineId"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划上线时间" prop="planOnlineTime">
              <el-date-picker clearable
                              v-model="form.planOnlineTime"
                              type="datetime"
                              placeholder="请选择计划上线时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划下线时间" prop="planOfflineTime">
              <el-date-picker clearable
                              v-model="form.planOfflineTime"
                              type="datetime"
                              placeholder="请选择计划下线时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProcessproPlan,
  getProcessproPlan,
  delProcessproPlan,
  addProcessproPlan,
  updateProcessproPlan,
  handleRead
} from "@/api/pcplan/processproPlan";
import {listProductionLine} from "@/api/base/productionLine";
import moment from "moment/moment";

export default {
  name: "ProcessproPlan",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      options: [{
        value: '下料',
        label: '下料'
      }, {
        value: '锻造',
        label: '锻造'
      }, {
        value: '机加',
        label: '机加'
      }],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 加工排产计划表格数据
      processproPlanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      lineList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoryCode: null,
        factoryName: null,
        orderNo: null,
        choose: null,
        contractNo: null,
        pcCode: null,
        pcDate: null,
        scDate: null,
        jhDate: null,
        lineCode: null,
        lineName: null,
        gwCode: null,
        gwName: null,
        deviceNo: null,
        deviceName: null,
        empNum: null,
        empName: null,
        proType: null,
        gxProCode: null,
        proName: null,
        dyProCode: null,
        dyYclCode: null,
        dyZcNum: null,
        orderNum: null,
        noPcNum: null,
        pcNum: null,
        finishComNum: null,
        zzNum: null,
        checkNum: null,
        unqualifiedNum: null,
        returnNum: null,
        flNum: null,
        jfNum: null,
        planOnlineTime: null,
        actualOnlineTime: null,
        planOfflineTime: null,
        actualOfflineTime: null,
        returnOnlineTime: null,
        returnOfflineTime: null,
        checkTime: null,
        checkUserNum: null,
        checkUserName: null,
        packgeTime: null,
        hxTime: null,
        lineGy: null,
        createDate:new Date()
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询加工排产计划列表 */
    getList() {
      this.loading = true;
      this.queryParams.createDate = moment.utc(this.queryParams.createDate).local().format('YYYY-MM-DD')
      if(this.queryParams.createDate == 'Invalid date'){
        this.queryParams.createDate = null
      }
      listProcessproPlan(this.queryParams).then(response => {
        this.processproPlanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    tableRowClassName({row, rowIndex}) {
      if (row.isRead == 0) {
        return 'warning-row';
      } else{
        return '';
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryCode: null,
        factoryName: null,
        orderNo: null,
        choose: null,
        contractNo: null,
        pcCode: null,
        pcDate: null,
        scDate: null,
        jhDate: null,
        lineCode: null,
        lineName: null,
        gwCode: null,
        gwName: null,
        deviceNo: null,
        deviceName: null,
        empNum: null,
        empName: null,
        proType: null,
        gxProCode: null,
        proName: null,
        dyProCode: null,
        dyYclCode: null,
        dyZcNum: null,
        orderNum: null,
        noPcNum: null,
        pcNum: null,
        finishComNum: null,
        zzNum: null,
        checkNum: null,
        unqualifiedNum: null,
        returnNum: null,
        flNum: null,
        jfNum: null,
        planOnlineTime: null,
        actualOnlineTime: null,
        planOfflineTime: null,
        actualOfflineTime: null,
        returnOnlineTime: null,
        returnOfflineTime: null,
        checkTime: null,
        checkUserNum: null,
        checkUserName: null,
        packgeTime: null,
        hxTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.lineGy = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加加工排产计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProcessproPlan(id).then(response => {
        listProductionLine(null).then(res=>{
          this.form = response.data;
          this.lineList = res.rows
          this.open = true;
          this.title = "修改加工排产计划";
        });
      });
    },
    handleRead() {
      this.reset();
      handleRead().then(response => {
        this.getList();
        this.$modal.msgSuccess("读取成功！");
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProcessproPlan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProcessproPlan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除加工排产计划编号为"' + ids + '"的数据项？').then(function() {
        return delProcessproPlan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pcplan/processproPlan/export', {
        ...this.queryParams
      }, `processproPlan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-table .warning-row {
  background: #ffe4e1;
}
</style>
